// TODO:save the solution to DB, has to be done here, because here we get the user identification

//This code is actually far too simplistic, a solution might already be found, so we don't need the users id, 
//but present him/her the id of the user who found it first

package com.googlecode.eightqueenshaggle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class VictoryViewController extends Activity implements
OnClickListener{

	public static final int MENU_NEW_GAME  = 1;
	public static final int MENU_NEW_GAME_CMD = 2;
	public static final int MENU_VIEW_SOLUTIONS = 3;
	public static final int MENU_QUIT = 4;
	
	Button buttonConfirm;
	EditText editTextPlayerName;
	
	Solution solution;
	Puzzle puzzle;
	SolutionObject solObj=null;
	
	public EightQueens eqHandler;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.victory);
	    
		eqHandler=(EightQueens) this.getApplication();
		eqHandler.victoryViewControllerHandler=this;
		solution=eqHandler.boardViewControllerHandler.solution;
		puzzle=eqHandler.boardViewControllerHandler.puzzle;

	    
	    buttonConfirm = (Button) findViewById(R.id.BT_Confirm);
	    editTextPlayerName = (EditText) findViewById(R.id.ET_PlayerName);

	    buttonConfirm.setOnClickListener(this);
	}
	
	/* Creates the menu items */
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

        menu.add(0, MENU_NEW_GAME, 0, "New Game");
        menu.add(0, MENU_NEW_GAME_CMD, 0, "New Game (cmd)");
        menu.add(0, MENU_VIEW_SOLUTIONS, 0, "View Solutions");
        menu.add(0, MENU_QUIT, 0, "Quit");
	    return true;
	}
	
	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case MENU_NEW_GAME:
	        newGame();
	        return true;
	    case MENU_NEW_GAME_CMD:
	        newGameCmd();
	        return true;
	    case MENU_VIEW_SOLUTIONS:
	        viewSolutions();
	        return true;
	    case MENU_QUIT:
	    	this.finish();
	        return true;
	    }
	    return false;
	}

	private void viewSolutions() {
		// TODO view Solutions from DB
		Intent intent = new Intent(this, SolutionExplorerViewController.class); 
        startActivity(intent); 
        finish();
	}

	private void newGameCmd() {
		//Intent intent = new Intent(this, EightQueens.class); 
        //startActivity(intent); 
        //finish();
	}

	private void newGame() {
		Intent intent = new Intent(this, BoardViewController.class); 
        startActivity(intent); 
        finish();
	}

	public void onClick(View v) {
		if (v == buttonConfirm) {
			String playerName=this.editTextPlayerName.getText().toString();
			
			//publish the solution.
			solObj=new SolutionObject(puzzle, solution, playerName);
			HaggleConnector.getInstance().pushSolutionObject(solObj);			
			
			Intent intent = new Intent(this, MainMenuController.class); 
	        startActivity(intent); 
	        finish();
		} 		
	}
}
